/**
 * Created by Administrator on 2017/9/25.
 */

$(document).ready(function () {



    var allId = '',clickId = '',sortFlag = 0;;
    var categoryId = locationSearcher('categoryId');
    
    var leftTpl = $('#cate-left-template').html();
    var leftCmp = Handlebars.compile(leftTpl);
    var rightTpl = $('#cate-right-template').html();
    var rightCmp = Handlebars.compile(rightTpl);

    //无数据模板
    var noneTpl = $('#none-data').html();
    var noneCmp = Handlebars.compile(noneTpl);

    var boxTpl = $('#cate-right-template').html();
    var boxCmp = Handlebars.compile(boxTpl);
    var start = 1,
        length = 10,
        maxnum = 0,
        num = 0,
        flag = 0,
        sort = '';

    getDataList();
    /*获取当前全部*/
    function getDataList() {
        if (flag){
            return;
        }
        flag = 1;
        $.ajax({
            url:C.interface.category,
            type:'post',
            dataType:'json',
            data:{
                categoryId:categoryId,
                start:start,
                length:length,
                lng:C.jingdu,
                lat:C.weidu,
                cityId:C.cityCode,
                sort:sort
            },
            success:function (response) {
                if (response.errorCode == '200'){
                    start ++;
                    var data = response.data;
                    var dataNum = data.stores.length;
                    maxnum = data.counts;
                    var title = data.parentCategory.name;
                    $('.title').html(title);
                    $('#cate-list-left-box').html(leftCmp(data));
                    /**
                     * 二级菜单栏默认选中第一个
                     * */
                    $('#cate-list-left-box').find('li').eq(0).addClass('cate-left-active');
                    allId = $('#cate-list-left-box').find('li').eq(0).data('id');
                    if (maxnum == '0'){
                        $('#cate-list-right-box').html(noneCmp(data));
                    }
                    if (dataNum > 0 ){
                        $('#cate-list-right-box').append(rightCmp(data));
                    }else {
                        num = maxnum +1;
                    }

                    /**
                     * 左侧栏点击切换数据信息
                     * */
                    $('.cate-left-list').bind('click',changeCategoryData);
                    /**
                     * 数据筛选
                     * */
                    $('.sort-method').unbind().bind('click',function () {
                        var that = $(this);
                        if (that.siblings().hasClass('sort-active')){
                            sortFlag = 0;
                            that.siblings().removeClass('sort-active');
                        }
                        if (sortFlag){
                            sortFlag = 0;
                            that.removeClass('sort-active');
                            sort = '';
                        }else {
                            sortFlag = 1;
                            sort = that.data('sort');
                            that.addClass('sort-active').siblings().removeClass('sort-active');

                        }
                        $('#cate-list-right-box').empty();
                        start = 1;
                        selectPullMethod();
                    });
                    lazyImg();
                    flag = 0;

                }else if (response.errorCode == '0'){
                    alert('登录过期请重新登录');
                    againLogin()
                }else {
                    alert(response.errorMsg);
                }
            }
        })
    }
    //监听滚动高度，加载数据
    $(window).on('scroll',function () {
        if (num>maxnum){
            return;
        }
        if ($(document).scrollTop() >= $(document).height() - $(window).height()) {
            selectPullMethod()
        }

    });

    /**
     * 上拉加载滚动时判断点击值得不同，选择不同的方法
     * */
    function selectPullMethod() {
        if (allId == clickId || clickId == ''){
            getDataList();
        }else {
            requestChangeCategory(clickId);
        }
    }

    /***
     *
     * 上拉加载更多推荐商家
     * */
    function pullLoadingMore() {
        if (flag){
            return ;
        }
        flag = 1;
        $('.no-info').hide();
        $('.loading').show();
        $.ajax({
            url:C.interface.homeShopMore,
            type:'post',
            dataType:'json',
            data:{
                start:start,
                length:length
            },
            success:function (response) {
                if (response.errorCode == '200'){
                    $('.loading').hide();
                    num++;
                    var data = response.data;
                    maxnum = data['count'];
                    start+=length;
                    if (maxnum == 0){
                        $('.no-info').show();
                    }
                    if (data.stores.length > 0){
                        console.log(data);
                        $('#cate-list-right-box').append(boxCmp(data));

                        new auiLazyload({
                            errorImage:'../img/errorimg.png'
                        });
                        // $('.menu-box').bind('click',jumpNewPage);
                    }else {
                        $('.no-info').show();
                        num = maxnum +1;
                    }

                    flag = 0;

                }else if (response.errorCode == '0'){
                    alert('登录过期请重新登录');
                    againLogin();
                }else {
                    alert(response.errorMsg);
                }
            }
        });
    }
    /*二级菜单切换*/
    function changeCategoryData() {
        var that = $(this);
        that.addClass('cate-left-active').siblings().removeClass('cate-left-active');
        var id = that.data('id');
        clickId = id;
        start = 1;
        num = 1;
        $('#cate-list-right-box').empty();
        selectPullMethod();
    }

    /*二级菜单分类的切换*/
    function requestChangeCategory(categoryId) {
        if (flag){
            return ;
        }
        flag = 1;
        $('.no-info').hide();
        $('.loading').show();
        $.ajax({
            url:C.interface.erjiCateChange,
            type:'get',
            dataType:'json',
            data:{
                start:start,
                length:length,
                sonCategoryId:categoryId,
                sort:sort
            },
            success:function (response) {
                if (response.errorCode == '200'){
                    start++;

                    var data = response.data;
                    console.log(data);
                    maxnum = response.count;
                    if (maxnum == 0){
                        $('#cate-list-right-box').html(noneCmp(data));
                    }
                    if (data.stores.length > 0){
                        $('#cate-list-right-box').append(rightCmp(data));
                        /**
                         * 数据筛选
                         * */
                        $('.sort-method').unbind.bind('click',function () {
                            var that = $(this);
                            if (that.siblings().hasClass('sort-active')){
                                sortFlag = 0;
                                that.siblings().removeClass('sort-active');
                            }

                            if (sortFlag){
                                sortFlag = 0;
                                that.removeClass('sort-active');
                                sort = '';

                            }else {
                                sortFlag = 1;
                                sort = that.data('sort');
                                that.addClass('sort-active').siblings().removeClass('sort-active');

                            }
                            $('#cate-list-right-box').empty();
                            start = 1;
                            selectPullMethod();
                        });

                        lazyImg();
                    }else {
                        num = maxnum +1;
                        //$('#cate-list-right-box').html(noneCmp(data));
                    }


                }
                flag = 0;
            }
        })
    }
    



    
});